﻿using System;
using System.Collections.Generic;
using Tau.DataAccess;
using Tau.Model;
using System.Linq;
using Tau.ViewModels;

namespace Tau.Service
{
    public partial class TauService
    {
        public Category AddCategory(Category category)
        {
            //map model to db object
            var adapter = new Adapters.Category();
            tblCategory dbObject = adapter.GetDbObject(category);

            //perform the add
            _context.tblCategories.AddObject(dbObject);
            _context.SaveChanges();

            //return model
            return adapter.GetModelObject(dbObject);
        }

        public void UpdateCategory(Category category)
        {
            //get existing page 
            tblCategory dbCategory = _context.tblCategories.SingleOrDefault(c => c.categoryID == category.CategoryId);

            dbCategory.category = category.CategoryName;

            _context.SaveChanges();
        }

        public List<Category> GetAllCategories()
        {
            List<tblCategory> dbCategoryList = _context.tblCategories.OrderBy(cat => cat.category).ToList();

            return new Adapters.Category().GetModelList(dbCategoryList);
        }

        public Category GetCategory(int categoryId)
        {
            tblCategory item = _context.tblCategories.SingleOrDefault(c => c.categoryID == categoryId);

            return new Adapters.Category().GetModelObject(item);
        }

        public void DeleteCategory(int categoryId)
        {
            //Delete the category
            tblCategory categoryToDelete = _context.tblCategories.SingleOrDefault(c => c.categoryID == categoryId);
            _context.tblCategories.DeleteObject(categoryToDelete);

            //Delete any associations between this category and products
            var itemCategories = _context.tblItemCategories.Where(ic => ic.categoryID == categoryId);
            foreach(var itemCategory in itemCategories)
            {
                _context.DeleteObject(itemCategory);
            }
            
            _context.SaveChanges();
        }

        public List<Category> GetCategoriesForProduct(int productId)
        {
            var categories = _context.tblItemCategories.Where(cat => cat.itemID == productId);

            return categories.Select(category => new Category()
                                                     {
                                                         CategoryId = category.categoryID, 
                                                         CategoryName = category.tblCategory.category
                                                     }).ToList();
        }


        /// <summary>
        /// Gets list of categories that have products in them
        /// </summary>
        /// <returns></returns>
        public List<Category> GetPublicCategories()
        {
            //Only get categories containing products
            var dbCategories = _context.tblCategories.Where(c => c.tblItemCategories.Count > 0).OrderBy(c => c.category).ToList();

            return new Adapters.Category().GetModelList(dbCategories);
        }
    }
}
